//
//  CBJStatuesPictureView.swift
//  Weibo
//
//  Created by 崔健 on 16/5/25.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit
//图片之间的间距
private let pictureMargin :CGFloat = 3
//图片视图最大宽度
private let maxPictureViewW :CGFloat = kScreenWidth - 2 * margin
// 图片宽度
private let pictureW :CGFloat = (maxPictureViewW - 2 * pictureMargin)/3

class CBJStatuesPictureView: UICollectionView {

    lazy var testLabel = UILabel()
    
     var pictureList : [CBJPicture]?{
    
        didSet{
        
            testLabel.text = "\(pictureList?.count)"
            
            let size = caculatePicstureSize()
            self.snp_updateConstraints { (make) in
                
                make.size.equalTo(size)
            }
            
            reloadData()
        }
  
    }
    
    func setupUI(){
    
        
        addSubview(testLabel)
        testLabel.textColor = UIColor.redColor()
        testLabel.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        
        super.init(frame: frame, collectionViewLayout: layout)

        
        
        // 取消点击状态栏会到顶部
        self.scrollsToTop = false

        dataSource = self
        backgroundColor = UIColor.whiteColor()
        setupUI()
        registerClass(CBJStatuesPictureCell.self, forCellWithReuseIdentifier: statuesPictureItemId)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
     1. 单张图片按照图片的原比例来显示
     2. 四张图片按照 2 * 2 对称显示
     3. 其他的多张图片 n * 3 形式来显示
     */
    
    func caculatePicstureSize() -> CGSize{
    
        let imageCount = (pictureList?.count) ?? 0
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        if imageCount == 0 {
            
            return CGSizeZero
        }
        
        if imageCount == 1 {
            
            let size  = CGSize(width: 100, height: 150)
            layout.itemSize = size
            return size
        }
        
        
        layout.itemSize = CGSize(width: pictureW, height: pictureW)

        if imageCount == 4 {
            
            return CGSize(width: 2 * pictureW + pictureMargin, height: 2 * pictureW + pictureMargin)
        }
        
        let lineNum = CGFloat((imageCount - 1)  / 3) + 1
        
        return CGSize(width: maxPictureViewW, height: pictureW * lineNum + (lineNum - 1) * pictureMargin)
        
        
    }
    
}

extension CBJStatuesPictureView : UICollectionViewDataSource{


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return (pictureList?.count) ?? 0
    }
    
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let item = dequeueReusableCellWithReuseIdentifier(statuesPictureItemId, forIndexPath: indexPath) as! CBJStatuesPictureCell

        item.picture = pictureList?[indexPath.item].thumbnail_pic ?? ""

        return item
    }
    
}


class CBJStatuesPictureCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        addSubview(pictureImageView)
        pictureImageView.contentMode = .ScaleAspectFill
        pictureImageView.clipsToBounds = true
        pictureImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(self)
        }
    }
    
    private var picture : String?{
    
        didSet{
        
            let url = NSURL(string: picture ?? "")
            pictureImageView.sd_setImageWithURL(url)
        }
    }
    
    private  var pictureImageView :UIImageView = UIImageView()
}

